package com.android.baseconfig.common.utils;

import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

/**
 * @author lei
 * @description ViewUtils
 * @date 2018/5/7
 */
public class ViewUtils {
    /**
     * 把自身从父View中移除
     */
    public static void removeSelfFromParent(View view) {
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent != null && parent instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) parent;
                group.removeView(view);
            }
        }
    }

    /**
     * 请求View树重新布局，用于解决中层View有布局状态而导致上层View状态断裂
     */
    public static void requestLayoutParent(View view, boolean isAll) {
        ViewParent parent = view.getParent();
        while (parent != null && parent instanceof View) {
            if (!parent.isLayoutRequested()) {
                parent.requestLayout();
                if (!isAll) {
                    break;
                }
            }
            parent = parent.getParent();
        }
    }

    /**
     * 判断触点是否落在该View上
     */
    public static boolean isTouchInView(MotionEvent ev, View v) {
        int[] vLoc = new int[2];
        v.getLocationOnScreen(vLoc);
        float motionX = ev.getRawX();
        float motionY = ev.getRawY();
        return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth()) && motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());
    }

    /**
     * FindViewById的泛型封装，减少强转代码
     */
    public static <T extends View> T findViewById(View layout, int id) {
        return (T) layout.findViewById(id);
    }

    /**
     * 设置view 是否显示
     *
     * @param v       需要设置的界面
     * @param Visible View.VISIBLE | View.GONE | View.INVISIBLE
     */
    public static void setViewVisible(View v, int Visible) {
        if (v != null && v.getVisibility() != Visible) {
            v.setVisibility(Visible);
        }
    }

    /**
     * 设置view 是否可点击
     *
     * @param v       需要设置的界面
     * @param enabled
     */
    public static void setEnabled(View v, boolean enabled) {
        if (v != null && v.isEnabled() != enabled) {
            v.setEnabled(enabled);
        }
    }

    /**
     * 代码控制哪个控件至于v控件的下方
     *
     * @param parentLayout 根layout
     * @param scrollview   滑动条
     * @param v            哪个控件的下方
     */
    public static void checkKeyboard(final View parentLayout, final ScrollView scrollview, final View v) {
        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        boolean isKeyBoardVisible = false;//此处需要修改
                        Rect r = new Rect();
                        parentLayout.getWindowVisibleDisplayFrame(r);
                        int screenHeight = parentLayout.getRootView().getHeight();
                        int heightDifference = screenHeight - (r.bottom);
                        if (heightDifference > 100) {
                            if (!isKeyBoardVisible) {//此处需要修改
                                int[] location = new int[2];
                                v.getLocationOnScreen(location);
                                int offset = location[1] + v.getHeight() / 2 - scrollview.getMeasuredHeight();
                                if (offset < 0) {
                                    offset = 0;
                                }
                                scrollview.smoothScrollTo(0, offset);
                            }
                            isKeyBoardVisible = true;//此处需要修改
                        } else {
                            isKeyBoardVisible = false;//此处需要修改
                        }
                    }
                });
    }

    public static boolean inRangeOfView(View view, MotionEvent ev){
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        return !(ev.getX() < x) && !(ev.getX() > (x + view.getWidth())) && !(ev.getY() < y) && !(ev.getY() > (y + view.getHeight()));
    }
}
